<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        /* 文件上传样式 */
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 150px;
            height: 150px;
            line-height: 150px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-form {
            padding-top: 7%;
            padding-left: 35%;
            padding-right: 40%;
        }

        .consumerHeadPortrait {
            padding-left: 20%;
        }

        .register {
            padding-right: 25%;
            float: right;
        }
    </style>
</head>

<body>
    <div id="app">
        <el-page-header @back="goBack" content="返回首页">
        </el-page-header>
        <el-form ref="consumer" :model="consumer" label-width="80px">
            <el-form-item label="" class="consumerHeadPortrait">
                <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
                    :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="consumer.consumerHeadPortrait" :src="consumer.consumerHeadPortrait" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="">
                <el-input placeholder="请输入用户名" v-model="consumer.consumerName" clearable>
                </el-input>
            </el-form-item>
            <el-form-item label="">
                <el-input placeholder="请输入密码" v-model="consumer.consumerPassword" show-password></el-input>
            </el-form-item>
            <el-form-item label="">
                <el-input placeholder="请输入手机号" v-model="consumer.phoneNumber" clearable>
                </el-input>
            </el-form-item>
            <el-form-item label="">
                <el-input placeholder="QQ邮箱" v-model="consumer.consumerEmail" clearable>
                </el-input>
            </el-form-item>

            <el-form-item class="register">
                <a v-bind:href="home" id="home" hidden></a>
                <el-button type="primary" @click="addConsumer(consumer)" > 立即创建</el-button>
            </el-form-item>
        </el-form>

    </div>
</body>
<script>
    let vm = new Vue({
        el: '#app',
        data: {
            home: 'http://localhost:63342/blog/blog-client-controller/web/WEB-INF/home.html?_ijt=k4rfimk6cmev0ev3e0gamkatpr',
            byConsumerUuidGetConsumerInfoVO:{
                consumerUuid: ''
            },
            consumer: {
                consumerName: '',
                consumerPassword: '',
                phoneNumber: '',
                consumerEmail: '',
                consumerHeadPortrait: ''
            }
        },
        methods: {
            goBack() {
                document.getElementById('home').click();
            },
            addConsumer(consumer) {
                axios.post('http://127.0.0.1:8181/consumer/add-consumer',consumer).then(
                    e => {
                        this.byConsumerUuidGetConsumerInfoVO.consumerUuid = e.data
                        if (e.data.length > 0){
                            axios.post('http://127.0.0.1:8181/consumer/by-consumer-uuid-get-consumer-info',this.byConsumerUuidGetConsumerInfoVO).then(e => {
                                localStorage.setItem("consumer",JSON.stringify(e.data))
                            })
                           document.getElementById('home').click();
                        }
                    }
                )
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    })
</script>

</html>